<cfset BASE = CreateObject("component", "EPARTermination.cfc.base") />
<cfset mainQueries = CreateObject("component", "EPARTermination.cfc.queries") />
<cfset termQueries = CreateObject("component", "EPARTermination.cfc.terminationQueries") />
<cfset statusQueries = CreateObject("component", "EPARTermination.cfc.statusQueries") />
<cfset term = CreateObject("component", "EPARTermination.cfc.Termination") /> 
<cfset Session.SERVICEDESK_EMAIL = 'bfrick@scf.cc' />
<cfset session.ITS_EMAIL = 'bfrick@scf.cc' />
<cfinclude template="/code_lib/SCF_Employee.cfm">
<cfinclude template="/code_lib/SCF_ActiveDirectory.cfc">
<cfinclude template="/code_lib/SCF_HR.cfm">
<cfinclude template="/code_lib/SCF_REGEX.cfm">
<cfset LDAP_Host = "10.100.1.101" />
<cfset LDAP_User = "scf\Userlist" />
<cfset LDAP_Pass = "DefinedMan" />
<cfset LDAP_Start = "dc=SouthcentralFoundation,dc=com" />

<cfoutput>

    <cfset QUERIES.GetOUTermSchedule = termQueries.funcGetOUTermSchedule(  ) />
    
    <Cfquery dbtype="query" name="QUERIES.ScheduledOUTerm">
        SELECT * FROM QUERIES.GetOUTermSchedule
        WHERE DeleteDate <= '#DateFormat( Now(), "m/d/yyyy" )#'
    </cfquery>
    
    <cfset summary = "" />    
    
	<cfloop query="QUERIES.ScheduledOUTerm">
		<cftry> 
		    <cfsetting enablecfoutputonly="true">
		    
            <cfldap action="DELETE" server="#LDAP_Host#" DN="#QUERIES.ScheduledOUTerm.ArchivedUserOU#" username="#LDAP_User#" password="#LDAP_Pass#"/>
			<cfquery name="updateRecord" datasource="EPAR">
		        UPDATE ArchivedUserOUTermSchedule
		        SET Completed = 1
		        WHERE ID = #QUERIES.ScheduledOUTerm.ID#
		    </cfquery>            
            <strong>UserDN:</strong> #ArchivedUserOU# <br /> <strong>Deleted:</strong> #DateFormat( Now(), 'm/d/yyyy' )#<hr /><br /><br />
	        <cfset summary &= "<strong>UserDN:</strong> #ArchivedUserOU# <br /> <strong>Deleted:</strong> #DateFormat( Now(), 'm/d/yyyy' )#<hr /><br /><br />" />            
	        <cfcatch type="any">	            
                <cfmail to=#Session.ITS_EMAIL# From="EPAR_Error@scf.cc" subject="ERROR: EPAR Archived OU Termination Summary" type="html">
                    <strong>An error has occured during the deletion of archived users.  Review the error below.</strong><br /><br />
                    <strong>Error Message:</strong> #CFCATCH.message#<br />
                    <strong>Error Detail:</strong> #CFCATCH.detail#                    
                </cfmail>                                                    
	        </cfcatch>
        </cftry>        
    </cfloop>

    <cfmail to=#Session.ITS_EMAIL# From="EPAR_Scheduled@scf.cc" subject="EPAR Archived OU Termination Summary" type="html">
        <strong>The archived user deletion completed without error.  A summary is provided below.</strong><br /><br />
        #summary#
    </cfmail>
    
    Finished!

</cfoutput>